<template>
  <div>
    <!-- 经销商库存实时查询 -->
    <TopFilter ref="vehicleStockFilter" :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTables
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :isSelectColumn="false"
        columnsRef="zjDealerInventory"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="(columns) => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle()">导 出</multiuse-button>
        </template>
      </FilterTables>
    </Spin>

    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SearchDealer :rowData="formData" @close="emitCloseHandler1" />
    </BaseDialog>
  </div>
</template>
<script>
import {
  getList,
  vehicleConfiguration,
  exportOem,
  exporturl,
  getBrandAll,
  getSeriesAll,
  getModelAll,
  getConfigAll,
  getColorAll,
  getTrimAll,
} from '@/api/omSale/StoreInventory/DealerInventory/index.js';
import { dictionary } from '@/utils/dictMixin';
import { exportFile } from '@/utils';
import SearchDealer from '../ownerSeacher';
export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        isDealer: 0,
        isSelect: 10041001,
        xhrAbort: true,
      },
      exportLoading: false,
      fetchApi: getList,
      formData: {},
      issearch: false,
      isDealer: false,
    };
  },
  components: {
    SearchDealer,
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getBrand();
    this.getColorTrim();
  },

  methods: {
    emitCloseHandler1(val, data) {
      this.isDealer = val;
      if (data) {
        let dealerCodes = [];
        let dealerNames = [];
        data.map((x) => {
          dealerCodes.push(x.dealerCode);
          dealerNames.push(x.dealerName);
        });
        this.topFilterList.find((x) => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
    },
    createdFrom() {
      return [
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          maxlength: '10000',
          placeholder: '输入多个VIN时，请用英文逗号或者换行隔开（例如：VIN1,VIN2……）',
        },
        {
          type: 'INPUT',
          label: '公司代码',
          fieldName: 'ownerCode',
          placeholder: '',
          maxlength: '',
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          readonly:true,
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => {this.isDealer = true}}></el-button>
              </div>
            );
          },
        },
        // {
        //   type: 'INPUT',
        //   label: '经销商代码',
        //   fieldName: 'dealerCode',
        //   placeholder: '输入多个经销商代码时，请用英文逗号隔开',
        // },
        { type: 'SELECT', label: '库存状态', fieldName: 'stockStatus', filterable: true, placeholder: '请选择', itemList: this.createDlrDictList('2652') },
        { type: 'INPUT', label: '物料编码', fieldName: 'productCode', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '物料描述', fieldName: 'productName', placeholder: '请输入', initialValue: '' },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brandCode',
          disabled: false,
          filterable: true,
          placeholder: '请选择',
          itemList: [],
          change: (val) => {
            //调用方法
            this.changeSeriesList(val);
          },
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'seriesCode',
          disabled: false,
          placeholder: '请选择',
          filterable: true,
          itemList: [],
          change: (val) => {
            //调用方法
            this.changeModelList(val);
          },
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'modelCode',
          disabled: false,
          placeholder: '请选择',
          filterable: true,
          itemList: [],
          change: (val) => {
            //调用方法
            this.changeConfigList(val);
          },
        },
        {
          type: 'SELECT',
          label: '配置',
          fieldName: 'configCode',
          disabled: false,
          filterable: true,
          placeholder: '请选择',
          itemList: [],
        },
        { type: 'SELECT', label: '外饰', fieldName: 'colorCode', filterable: true, disabled: false, placeholder: '请选择', itemList: [] },
        { type: 'SELECT', label: '内饰', fieldName: 'trimCode', filterable: true, disabled: false, placeholder: '请选择', itemList: [] },
        { type: 'INPUT', label: '零售订单号', fieldName: 'soNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '采购订单号', fieldName: 'doNo', placeholder: '请输入', initialValue: '' },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        { title: '公司代码', dataIndex: 'ownerCode', width: '180px', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '经销商代码', dataIndex: 'dealerCode', width: '180px', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '经销商名称', dataIndex: 'dealerName', width: '180px', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: 'VIN', dataIndex: 'vin', width: '180px', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        {
          title: '仓库',
          dataIndex: 'storageName',
          showOverflowTooltip: true,
          sorter: false,
          filter: false,
          align: 'left',
          filterType: 'input',
          // render: props => {
          //   return <span>{this.createDictText(props.row.inspectionResult, '2655')}</span>;
          // }
        },
        {
          title: '库存状态',
          dataIndex: 'stockStatus',
          showOverflowTooltip: true,
          sorter: false,
          filter: false,
          align: 'left',
          filterType: 'input',
          render: (props) => {
            return <span>{this.createDlrDictText(props.row.stockStatus, '2652')}</span>;
          },
        },
        {
          title: '配车状态',
          dataIndex: 'dispatchedStatus',
          showOverflowTooltip: true,
          sorter: false,
          filter: false,
          align: 'left',
          filterType: 'input',
          render: (props) => {
            return <span>{this.createDlrDictText(props.row.dispatchedStatus, '2653')}</span>;
          },
        },
        {
          title: '质损状态',
          dataIndex: 'marStatus',
          showOverflowTooltip: true,
          sorter: false,
          filter: false,
          align: 'left',
          filterType: 'input',
          render: (props) => {
            return <span>{this.createDlrDictText(props.row.marStatus, '2656')}</span>;
          },
        },
        { title: '公告号', dataIndex: 'announceNo', width: '180px', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '物料编码', dataIndex: 'productCode', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '物料描述', dataIndex: 'productName', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '品牌', dataIndex: 'brandName', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '车系', dataIndex: 'seriesName', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '车型', dataIndex: 'modelName', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '配置', dataIndex: 'configName', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '选装', dataIndex: 'optionName', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '内饰', dataIndex: 'trimName', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '外饰', dataIndex: 'colorName', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '发动机号', dataIndex: 'engineNo', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '电机号', dataIndex: 'eleEngineNo', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '电池号', dataIndex: 'batteryNo', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '采购订单号', dataIndex: 'doNo', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        // { title: '采购日期', dataIndex: 'vsPurchaseDate', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input', dateFormat: 'yyyy-MM-dd' },
        // { title: '采购价格', dataIndex: 'purchasePrice', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '销售指导价', dataIndex: 'directivePrice', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        {
          title: '最新入库业务类型',
          dataIndex: 'stockInType',
          showOverflowTooltip: true,
          sorter: false,
          filter: false,
          align: 'left',
          filterType: 'input',
          render: (props) => {
            return <span>{this.createDlrDictText(props.row.stockInType, '2650')}</span>;
          },
        },
        { title: '首次入库时间', dataIndex: 'firstStockInDate', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '最后出库时间', dataIndex: 'latestStockOutDate', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        {
          title: '最新出库类型',
          dataIndex: 'stockOutType',
          showOverflowTooltip: true,
          sorter: false,
          filter: false,
          align: 'left',
          filterType: 'input',
          render: (props) => {
            return <span>{this.createDlrDictText(props.row.stockOutType, '2651')}</span>;
          },
        },
        {
          title: '是否三方车辆',
          dataIndex: 'isBill',
          showOverflowTooltip: true,
          sorter: false,
          filter: false,
          align: 'left',
          filterType: 'input',
          render: (props) => {
            return <span>{this.createDictText(props.row.isBill, '1004')}</span>;
          },
        },
        { title: '三方票号', dataIndex: 'billNo', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        {
          title: '是否有合格证',
          dataIndex: 'hasCertificate',
          showOverflowTooltip: true,
          sorter: false,
          filter: false,
          align: 'left',
          filterType: 'input',
          render: (props) => {
            return <span>{this.createDictText(props.row.hasCertificate, '1004')}</span>;
          },
        },
        { title: '合格证号', dataIndex: 'certificateNo', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '送车人姓名', dataIndex: 'driverName', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '送车人电话', dataIndex: 'driverTel', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '承运商名称', dataIndex: 'carrierName', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '承运车牌号', dataIndex: 'trailerBrand', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '收货地址', dataIndex: 'address', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '货运单号', dataIndex: 'shippingOrderNo', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '零售订单号', dataIndex: 'soNo', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: 'SAP交货单号', dataIndex: 'sapNo', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '车龄', dataIndex: 'carAge', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '库龄', dataIndex: 'stockAge', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        { title: '生产日期', dataIndex: 'productDate', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input', dateFormat: 'yyyy-MM-dd' },
        // { title: '发车日期', dataIndex: 'deliveryDate', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input', dateFormat: 'yyyy-MM-dd' },
        // { title: '出厂日期', dataIndex: 'factoryDate', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input', dateFormat: 'yyyy-MM-dd' },
        { title: '实际送到日期', dataIndex: 'arrivedDate', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input', dateFormat: 'yyyy-MM-dd' },
        // { title: '验收日期', dataIndex: 'pdiEntryDate', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input', dateFormat: 'yyyy-MM-dd' },
        // {
        //   title: '验收人员',
        //   dataIndex: 'inspector',
        //   showOverflowTooltip: true,
        //   sorter: false,
        //   filter: false,
        //   align: 'left',
        //   filterType: 'input',
        //   render: props => {
        //     return <span>{this.findInspectorName(props.row.inspector)}</span>;
        //   }
        // },
        // {
        //   title: '验收人员',
        //   dataIndex: 'pdiEntryPerson',
        //   showOverflowTooltip: true,
        //   sorter: false,
        //   filter: false,
        //   align: 'left',
        //   filterType: 'input'
        // },
        {
          title: '验收结果',
          dataIndex: 'isFinished',
          showOverflowTooltip: true,
          sorter: false,
          filter: false,
          align: 'left',
          filterType: 'input',
          render: (props) => {
            return <span>{this.createDlrDictText(props.row.isFinished, '1004')}</span>;
          },
        },
        { title: '备注', dataIndex: 'remark', showOverflowTooltip: true, sorter: false, filter: false, align: 'left', filterType: 'input' },
        // {
        //   title: '订单细分类型',
        //   dataIndex: 'orderDetailTypeName',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // }
      ];
    },
    selectChangeHandle(row) {},
    async getList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find((item) => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    seacherFun() {
      this.issearch = true;
    },
    async addHandle(data) {
      let json = Object.assign({}, { CODE: 'CODE_1000002' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
    },
    changeHandle(params) {
      // this.fetchParams.xhrAbort = false;
      // this.fetchParams = Object.assign({}, this.fetchParams, params);
      console.log('changeHandle', params);
      this.fetchParams = Object.assign({}, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.emitvisible = true;
    },
    emitcloseHandler(val, data) {
      if (data) {
        s;
        let dealerCodes = [];
        data.map((x) => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find((x) => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
      this.issearch = false;
    },
    resetChange() {
      // this.fetchParams = Object.assign({}, this.fetchParams);
      this.fetchParams.xhrAbort = true;
      this.topFilterList.forEach((item) => (item.initialValue = ''));
    },
    // 获取品牌
    async getBrand() {
      const res = await getBrandAll();
      if (!res || !res.data) return;
      this.topFilterList.find((item) => item.fieldName === 'brandCode').itemList = res.data.map((x) => ({
        text: x.brandName,
        value: x.brandCode,
      }));
    },
    //品牌改变时查询车系
    async changeSeriesList(brandCode) {
      this.topFilterList.find((item) => item.fieldName === 'seriesCode').initialValue = null;
      this.topFilterList.find((item) => item.fieldName === 'modelCode').initialValue = null;
      this.topFilterList.find((item) => item.fieldName === 'configCode').initialValue = null;

      this.topFilterList.find((item) => item.fieldName === 'seriesCode').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'modelCode').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'configCode').itemList = [];

      if (!brandCode) return;
      console.log('11111');
      let requestDTO = {
        data: {
          brandCode: brandCode,
        },
      };
      const res = await getSeriesAll(requestDTO);
      let seriesDict = res.data.map((x) => ({
        text: x.seriesName,
        value: x.seriesCode,
      }));
      this.topFilterList.find((item) => item.fieldName === 'seriesCode').itemList = seriesDict;
      //为了支持多级下拉框联动搜索功能，需要给子组件的参数赋值
      if (this.$refs.vehicleStockFilter.seriesCodeOriginItemList) {
        this.$refs.vehicleStockFilter.seriesCodeOriginItemList = seriesDict;
      }
    },
    //车系改变时查询车型
    async changeModelList(seriesCode) {
      this.topFilterList.find((item) => item.fieldName === 'modelCode').initialValue = null;
      this.topFilterList.find((item) => item.fieldName === 'configCode').initialValue = null;

      this.topFilterList.find((item) => item.fieldName === 'modelCode').itemList = [];
      this.topFilterList.find((item) => item.fieldName === 'configCode').itemList = [];
      if (!seriesCode) return;
      let requestDTO = {
        data: {
          seriesCode: seriesCode,
          onSale: 10041001,
        },
      };
      const res = await getModelAll(requestDTO);
      let modelDict = res.data.map((x) => ({
        text: x.modelName,
        value: x.modelCode,
      }));
      this.topFilterList.find((item) => item.fieldName === 'modelCode').itemList = modelDict;
      //为了支持多级下拉框联动搜索功能，需要给子组件的参数赋值
      if (this.$refs.vehicleStockFilter.modelCodeOriginItemList) {
        this.$refs.vehicleStockFilter.modelCodeOriginItemList = modelDict;
      }
    },
    //车型改变时查询配置
    async changeConfigList(modelCode) {
      this.topFilterList.find((item) => item.fieldName === 'configCode').initialValue = null;
      this.topFilterList.find((item) => item.fieldName === 'configCode').itemList = [];
      if (!modelCode) return;
      let requestDTO = {
        data: {
          modelCode: modelCode,
          onSale: 10041001,
        },
      };

      const res = await getConfigAll(requestDTO);
      let configDict = res.data.map((x) => ({
        text: x.configName,
        value: x.configCode,
      }));
      this.topFilterList.find((item) => item.fieldName === 'configCode').itemList = configDict;
      if (this.$refs.vehicleStockFilter.configCodeOriginItemList) {
        this.$refs.vehicleStockFilter.configCodeOriginItemList = configDict;
      }
    },
    //颜色 内饰
    async getColorTrim() {
      const color = await getColorAll();
      const trim = await getTrimAll();
      this.topFilterList.find((item) => item.fieldName === 'colorCode').itemList = color.data.map((x) => ({
        text: x.colorName,
        value: x.colorCode,
      }));
      this.topFilterList.find((item) => item.fieldName === 'trimCode').itemList = trim.data.map((x) => ({
        text: x.trimName,
        value: x.trimCode,
      }));
    },
  },
};
</script>
